import util.GameTime;
import flash.net.*;
import flash.events.*;	
import mx.utils.*;		
import mx.controls.Alert;
import flash.events.EventDispatcher;  
import spark.components.Image;
import mx.controls.Label;
import mx.events.FlexEvent;
import poker.*;
import spark.components.Group;
import spark.utils.TextFlowUtil;
import controller.login.LoginController;
import controller.lobby.LobbyController;
import controller.room.RoomController;
import controller.game.GameController;
import controller.ControllerManager;
import controller.SocketManager;
//public var sender:lobbyChatSend=new lobbyChatSend();
var timecount:int=0;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
	/*方块3 :(3-3)*4+1-1 =0
	梅花3:(3-3)*4+2-1=1
	红桃3 :(3-3)*4+3-1=2
	黑桃3:(3-3)*4+4-1=3
	
	方块4 :(4-3)*4+1-1 =4
	梅花4:(4-3)*4+2-1=5
	红桃4 :(4-3)*4+3-1=6
	黑桃4:(4-3)*4+4-1=7
	
	方块5 :(5-3)*4+1-1 =8
	梅花5:(5-3)*4+2-1=9
	红桃5 :(5-3)*4+3-1=10
	黑桃5:(5-3)*4+4-1=11
	
	方块6 :(4-3)*4+1-1 =12
	梅花6:(4-3)*4+2-1=13
	红桃6 :(4-3)*4+3-1=14
	黑桃6:(4-3)*4+4-1=15
	
	方块7 :(4-3)*4+1-1 =16
	梅花7:(4-3)*4+2-1=17
	红桃7 :(4-3)*4+3-1=18
	黑桃7:(4-3)*4+4-1=19
	
	方块8 :(4-3)*4+1-1 =20
	梅花8:(4-3)*4+2-1=21
	红桃8 :(4-3)*4+3-1=22
	黑桃8:(4-3)*4+4-1=23
	
	方块9 :(4-3)*4+1-1 =24
	梅花9:(4-3)*4+2-1=25
	红桃9 :(4-3)*4+3-1=26
	黑桃9:(4-3)*4+4-1=27
	
	方块10 :(4-3)*4+1-1 =28
	梅花10:(4-3)*4+2-1=29
	红桃10 :(4-3)*4+3-1=30
	黑桃10:(4-3)*4+4-1=31
	
	方块j :(11-3)*4+1-1 =32
	梅花j:(11-3)*4+2-1=33
	红桃j :(11-3)*4+3-1=34
	黑桃j:(11-3)*4+4-1=35
	
	方块q :(11-3)*4+1-1 =36
	梅花q:(11-3)*4+2-1=37
	红桃q :(11-3)*4+3-1=38
	黑桃q:(11-3)*4+4-1=39
	
	方块k :(11-3)*4+1-1 =40
	梅花k:(11-3)*4+2-1=41
	红桃k :(11-3)*4+3-1=42
	黑桃k:(11-3)*4+4-1=43
	
	方块A :(11-3)*4+1-1 =44
	梅花A:(11-3)*4+2-1=45
	红桃A :(11-3)*4+3-1=46
	黑桃A:(11-3)*4+4-1=47
	
	方块2 :(11-3)*4+1-1 =48
	梅花2:(11-3)*4+2-1=49
	红桃2 :(11-3)*4+3-1=50
	黑桃2:(11-3)*4+4-1=51
	
	小王 :(11-3)*4+1-1 =52
	大王:(11-3)*4+2-1=53			
	*/
}
public function funTimer():void{
	timecount++;
	trace(timecount);
}

// 构建大厅中初始的15张桌子
// 如果桌子的数量发生变化，在这里进行维护和修改，所有的桌子采用group的方式
protected function gameRoomCanvas_creationCompleteHandler(event:FlexEvent):void
{
	const tableStartX:int = 30;
	const tableStartY:int = 20;
	var intervalX:int = 100;
	const intervalY:int = 80;
	const roomTableMax:int = 15;
	const roomTableRowMax:int = 3;
	const roomTableColumnMax:int = 4;
	const tableAvatarSize:int = 29;				
	
	
	for(var i:int =0; i<roomTableRowMax; i++)
	{
		for(var j:int =0; j<roomTableColumnMax; j++)
		{
			// 如果超出了界限，直接返回
			if(i*roomTableColumnMax+j >= roomTableMax)
				return;
			
			var id:int = i*roomTableColumnMax +j+1;
			
			// 所有的桌子的元素都将会被加到这个ui中去，加入顺序是
			// 0: 0号位椅子
			// 1: 桌子
			// 2: 1号位椅子
			// 3: 2号位椅子
			// 4: 3号位椅子
			// 5: 左上桌子的名字
			// 6: 0号位玩家头像，名字
			// 7: 1号位玩家头像，名字
			// 8: 2号位玩家头像，名字
			// 9: 3号位玩家头像，名字
			var group:Group = new Group();
			group.id = "ggrouptable_"+id.toString();
			group.x = tableStartX + (intervalX+51)*j;
			group.y = tableStartY + (intervalY+52)*i;
			group.visible = false;
			gameRoomCanvas.addElement(group);
			
			// 桌子的图
			var img:spark.components.Image = new spark.components.Image();
			img.x = 0;
			img.y = 0;
			img.id = "grouptable_"+id.toString();
			img.source="../media/desk_wait.png";	
			img.buttonMode=true;
			img.addEventListener(MouseEvent.CLICK, tableBtnHandler);
			group.addElementAt(img, 0);					
			// 创建按钮
			/* group.addElementAt(createTableBtn(id.toString(), "left"+id.toString(), img.x-18, img.y+48, 1), 2);
			group.addElementAt(createTableBtn(id.toString(), "down"+id.toString(), img.x+58, img.y+48, 2), 3);
			group.addElementAt(createTableBtn(id.toString(), "right"+id.toString(), img.x+58, img.y-12, 3), 4); */
			
			
			// 创建按钮
			group.addElementAt(createTableBtn(id.toString(), "left_"+id.toString(), img.x-30, img.y+16, 1), 1);
			group.addElementAt(createTableBtn( id.toString(), "down_"+id.toString(), img.x+12, img.y+50, 2), 2);
			group.addElementAt(createTableBtn(id.toString(), "right_"+id.toString(), img.x+52, img.y+16, 3), 3);
			
			group.addElementAt(createAvatarAndName("left_"+id.toString(), img.x-30, img.y+16), 4);
			group.addElementAt(createAvatarAndName("down_"+id.toString(), img.x+12, img.y+50), 5);
			group.addElementAt(createAvatarAndName("right_"+id.toString(), img.x+52, img.y+16), 6);	
			// 桌子上的桌号
			//						var tableName:Label = new Label();
			//						/* if(skin == 2)
			//						{
			//						tableName.x = img.x + 15;
			//						tableName.y = img.y - 40;
			//						} */
			//						tableName.x = img.x +25;
			//						tableName.y = img.y +25 ;							
			//						
			//						
			//						// 为了保证换行以后的文字能够都显示出来
			//						tableName.width = 50;
			//						tableName.height = 45;
			//						tableName.text = (id+1).toString();//String(obj[id].name).replace("台", "台\n");;//(id+1).toString();
			//						label.setStyle("fontSize", 14);
			//						label.setStyle("color","#ffcc33"); 
			//						group.addElementAt(tableName, 4);
			// 创建桌子上的头像图和文字信息
			// 0: 头像
			// 1: 文字信息
			//						if(skin==2)
			//						{
			//							group.addElementAt(LobyManager.Instance.createAvatarAndName("up"+id.toString(), img.x-18, img.y-12), 6);
			//							group.addElementAt(LobyManager.Instance.createAvatarAndName("left"+id.toString(), img.x-18, img.y+48), 7);
			//							group.addElementAt(LobyManager.Instance.createAvatarAndName("down"+id.toString(), img.x+58, img.y+48), 8);
			//							group.addElementAt(LobyManager.Instance.createAvatarAndName("right"+id.toString(), img.x+58, img.y-12), 9);							
			//						}
			
			//						group.addElementAt(createAvatarAndName("up"+id.toString(), img.x+17, img.y-40), 5);
								
			
			
		}			
	}	

}
public function createTableBtn(id:String, name:String, x:int, y:int, pos:int):spark.components.Image
{
	// 进行一定的修改，不再使用按钮，而是直接的椅子的图			
	var btn:spark.components.Image = new spark.components.Image();
	btn.source="../media/desk_empty.png";	
	btn.buttonMode=true;
	btn.x = x;
	btn.y = y;
	btn.id = id;
	btn.name = name;
	btn.addEventListener(MouseEvent.CLICK, tableBtnHandler);
	//			btn.width = 30;
	//			btn.height = 30;
	//			btn.setStyle("cornerRadius", 13);
	//			btn.setStyle("borderColor", "#445c95");
	return btn;
}
// 当桌子上的座位被点击了以后，会发出加入游戏的请求。
private function tableBtnHandler(event:MouseEvent):void
{
	// 点击台面的标签以后，对该页面的内容进行请求，并清空之前的内容				  
//	LobyErrorState.Instance.showErrMsg(event.currentTarget.id);
	RoomController.Instance.GAME_ROOM_JOIN(event.currentTarget.id, event.currentTarget.name);
//	return;
	
}
public function createAvatarAndName(name:String, x:int, y:int):Group
{
	// 增加头像的2个框
	var group:Group = new Group();
	/*group.graphics.beginFill(0x0, 1);
	group.graphics.drawRect(x-2, y-2, 29+4, 29+4);
	group.graphics.beginFill(0xeeeeee, 1);
	group.graphics.drawRect(x-1, y-1, 29+2, 29+2);
	group.graphics.endFill();*/
	
	var img:spark.components.Image = new spark.components.Image();
	img.name = "avatar_"+name;
	img.x = x;
	img.y = y;
	img.scaleX = 0.5;
	img.scaleY = 0.5;
	group.addElement(img);
	//			if(img.source != obj.avatar)
	//				img.source = obj.avatar;
	//			FlexGlobals.topLevelApplication.gameRoomCanvas.addChild(img);
	//			if(obj.ready)
	//				lbl.text = obj.name +"(o)";
	//			else
	//				lbl.text = obj.name +"(x)";
	//
	//			var describe:String = "";
	//			describe += obj.score;
	//			describe += "分 ";
	//			describe += LevelDefine.getLevelName(int(obj.score));
	//			describe += "\n";
	////			for each(var scoreobj:Object in obj.score)//StateGetPlayerInfo.Instance.lastSuccData.player.score)
	////			{
	////				describe += scoreobj.name;
	////				describe += " ";
	////				describe += scoreobj.score;
	////				describe += "分 ";
	////				describe += LevelDefine.getLevelName(int(scoreobj.score));
	////				describe += "\n";
	////			}
	//
	//			describe += obj.money + " 金币";//StateGetPlayerInfo.Instance.lastSuccData.player.money+" 金币";
	//			lbl.toolTip = lbl.text + "\n" + describe;
	
	var lbl:Label = new Label();
	lbl.name = "name_"+name;
	lbl.text = name;
	//			lbl.setStyle("color", 0xFFFFFF);
	lbl.width = 44;
	lbl.x = x;
	lbl.y = y+50;
	group.addElement(lbl);
	return group;
}

public function _startup():void
{

}

// 准备界面时，准备完成点击用按钮
private function gameReadyClicked(event:Event):void
{
	// 发送准备好的消息，如果失败，将会自动重发，
	poker.NetManager.Instance.send(poker.NetManager.send_iamReady);
	btnReady.enabled = false;
	// 点击准备按钮以后，消去这个按钮，然后显示玩家的头像在座位上
}


//出牌提示
protected function sendCardsClicked(event:Event):void
{
	GameController.Instance.sendCardsClicked(event);
	// 清空计数器
//	Gamestate.StateSendCards.Instance.clearCounter();
}

protected function discardClicked(event:Event):void
{
	GameController.Instance.discardClicked(event);
}


protected function btnHint_clickHandler(event:MouseEvent):void
{
	GameController.Instance.btnHint_clickHandler(event);
//	Game.Instance.hint();
}
